﻿$.fn.OrderGrid = function (options, param) {
    var me = this;
    //判断options是否是string类型
    if (typeof options == 'string') {
        //根据options从datagrid的配置中查找
        var method = $(this).datagrid('options')[options];
        //如果没找到，从$.fn.ProductGrid的methods中查找
        if (!method)
            method = $.fn.ProductGrid.methods[options];
        //如果存在，调用方法
        if (method)
            return method(this, param);
        else
            alert(options + 'can not support');
    }
    var defaults = {
        //---------------------------- 属性 Properties ----------------------------
        //请求类型 默认post
        method: "post",
        //请求url地址 默认null
        url: options.url,
        //是否开启隔行条纹 默认false
        striped: true,
        pagination: true,
        rowStyler: function (index, row) {
            if (row.TotalQty > 900) {
                return 'background-color:#6293BB';
                //这个方法支持return[class和样式对象]
                //return {class:'rowClass',style:'color:red'}
            }
        },
        //冻结列配置
        frozenColumns: [[
            { checkbox: true, field: 'ck' }
        ]],
        //列配置
        columns: [
            [
                { field: 'OrderNo', title: "订单编号", align: "left", halign: "center" },
                { field: 'TotalQty', title: "订单金额", align: "right", halign: "center" }
            ]
        ],
        queryParams: {
            //orderNo: OrderNo
        },
        onCheck: function (index, row) {

            var $total = $("#total");

            var result = $total.text() / 1 + row.TotalQty;
            $total.text(result);

        },
        onUncheck: function (index, row) {
            var $total = $("#total");

            var result = $total.text() / 1 - row.TotalQty;
            $total.text(result);
        },
        //复选全选选中事件
        onCheckAll: function (rows) {
            var $total = $("#total");
            var result = $total.text() / 1;
            for (var i = 0; i < rows.length; i++) {
                result += rows[i].TotalQty;
            }

            $total.text(result);
        },
        //复选全选取消选中事件
        onUncheckAll: function (rows) {
            var $total = $("#total");
            var result = $total.text() / 1;
            for (var i = 0; i < rows.length; i++) {
                result -= rows[i].TotalQty;
            }

            $total.text(result);
        },
        onBeforeLoad: function (param) {
            param.before = true;
            return param;
            //return false;
        },
        //附加的公共方法
        searchByID: function (jq, OrderNo) {
            $(me).datagrid('load', { OrderNo: OrderNo });
        }
    };
    options = $.extend(defaults, options);
    $(me).datagrid(options);
};

